<!--
 * @Author: Bobby
 * @Date: 2021-12-30 10:01:42
 * @LastEditTime: 2022-03-23 15:02:32
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\elements\basic\uiForm\uiForm.vue
 * CIMC-V2
-->
<template>
  <div
    :id="hasPreview ? info._id : `diy_${info._id}`"
    ref="chart"
    class="ui-line-chart h-full"
    :style="{
      width: '100%',
    }"
  ></div>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, RefsInjectMixin, ChartMixin } from '@ve/mixin'
  import { radarOptions } from './chartOptionJson.js'
  import cloneDeep from 'lodash/cloneDeep'
  import { radarResponse } from './chartResponseJson.js'
  import * as echarts from 'echarts'
  export default defineComponent({
    name: 'UiRadar',
    components: {},
    mixins: [UiMixin, RefsInjectMixin, ChartMixin],
    inject: {
      hasPreview: { default: false },
    },
    props: {
      info: {
        required: true,
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        infoComponentPie: {},
        inter: null,
      }
    },
    watch: {
      'info.extension': {
        handler() {
          this.$nextTick(() => {
            this.infoComponentPie = cloneDeep(this.info)
            this.getRequestData()
            this.inter && (clearInterval(this.inter), (this.inter = null))
            if (this.info.extension.requestSet.value.loopRequest) {
              this.inter = setInterval(() => {
                this.getRequestData()
              }, this.info.extension.requestSet.value.loopTime)
            }
          })
        },
        deep: true,
      },
      getSelectModule: {
        handler(val, oldVal) {
          if (!oldVal.extension) {
            if (radarOptions[val.chartTypeValue]) {
              this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = false)
            } else {
              this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = true)
            }
          }
          //不是相同module则重置
          if (
            val.extension &&
            oldVal.extension &&
            val._id != oldVal._id &&
            this.getSelectModule.extension &&
            this.getSelectModule.extension.requestSet
          ) {
            this.getSelectModule.extension.requestSet.value.isJson = false
          }
          //是示例module则覆盖
          if (
            radarResponse[this.getSelectModule.chartTypeValue] &&
            this.getSelectModule.extension &&
            this.getSelectModule.extension.requestSet &&
            !this.getSelectModule.extension.requestSet.value.isJson &&
            this.getSelectModule.extension.requestSet.value.resList.length == 0
          ) {
            this.getSelectModule.extension.requestSet.value.isJson = true
            this.getSelectModule.extension.requestSet.value.resList = radarResponse[val.chartTypeValue]
          }
        },
        deep: true,
      },
    },
    created() {
      this.infoComponentPie = cloneDeep(this.info)
    },
    mounted() {
      this.$nextTick(() => {
        this.getRequestData()
        if (this.info.extension.requestSet.value.loopRequest) {
          this.inter = setInterval(() => {
            this.getRequestData()
          }, this.info.extension.requestSet.value.loopTime)
        } else {
          this.inter && (clearInterval(this.inter), (this.inter = null))
        }
      })
    },
    unmounted() {
      this.inter && (clearInterval(this.inter), (this.inter = null))
    },
    methods: {
      getRequestData() {
        if (radarResponse[this.info.chartTypeValue] && this.infoComponentPie.extension.requestSet.value.resList.length == 0) {
          this.infoComponentPie.extension.requestSet.value.isJson = true
          this.infoComponentPie.extension.requestSet.value.resList = radarResponse[this.info.chartTypeValue]
        } else if (!radarResponse[this.info.chartTypeValue] && this.infoComponentPie.extension.requestSet.value.resList.length == 0) {
          this.infoComponentPie.extension.requestSet.value.resList.push({
            name: 'EDITWHO',
            id: '雷达图',
            value: 'QTY',
          })
        }
        let obj = {}
        if (!radarResponse[this.info.chartTypeValue] && this.infoComponentPie.extension.requestSet.value.resList.length == 0) {
          this.infoComponentPie.extension.requestSet.value.resList.push({
            name: 'EDITWHO',
            id: '雷达图',
            value: 'QTY',
          })
        }
        this.$request(this.infoComponentPie.extension.requestSet.value, (res) => {
          this.infoComponentPie.extension.requestSet.value['resList'].map((item) => {
            obj[item.id] = this.$getResponseData(res, item)
          })
          console.log(obj)
          this.initChart(obj)
        })
      },
      initChart(obj) {
        let dataObj = cloneDeep(obj)
        let chartDom = this.$refs.chart
        let myChart = echarts.init(chartDom)
        let data = cloneDeep(this.info.extension)
        let option = this.formatterOption(data)
        option.color = option.color.map((item) => item.v)
        const radarOptionss = cloneDeep(radarOptions)
        let series = radarOptionss[this.info.chartTypeValue]
        if (series) {
          if (this.info.chartTypeValue == 'baseRadar') {
            option = Object.assign(option, series(dataObj))
          } else {
            option = Object.assign(option, series(dataObj[this.info.chartTypeValue]))
          }
        }
        option.legend.textStyle.fontSize = this.info.extension.legend_textStyle_fontSize.value
        option.series.map((item) => {
          // item.data = dataObj[item.typeName || item.name]
          return item
        })
        console.log(option, this.getSelectModule.extension)
        option && myChart.setOption(cloneDeep(option), true)
      },
    },
  })
</script>
<style lang="scss">
  .uiRadar {
    height: 100%;
    width: 100%;
  }
</style>
